package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.MessageNoReadAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.bean.MessageListBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IMessageListView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class MessageListPresenter
{
    private MessageListBean messageListBean=new MessageListBean();
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private int requestType=0;
    private IMessageListView messageListView;
    private Context context;
    private BaseView baseView;
    private BaseBean baseBean=new BaseBean();
    private MessageNoReadAdapter messageNoReadAdapter;
    public MessageListPresenter(Context context, IMessageListView messageListView, BaseView baseView)
    {
        this.context = context;
        this.messageListView = messageListView;
        this.baseView=baseView;
    }

    /**
     * 消息列表
     */
    public void getMessage(String page)
    {
        requestType=1;
        baseView.showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getMessageList(UrlData.BASEURL + UrlData.MESSAGE, page, SharePrefencesUtil.getUser_id(context), "0", handler);
    }

    /**
     * 将所有未读消息设置为已读
     */
    public void setAllMessage(){
        requestType=2;
//        baseView.showLoading();
        MyOkHttpClient myOkHttpClient=new MyOkHttpClient(context,baseView);
        myOkHttpClient.setAllMessage(UrlData.BASEURL+UrlData.SETALLMESSAGE,SharePrefencesUtil.getUser_id(context),handler);
    }
    public MessageListBean getData()
    {

        return messageListBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            messageListView.onLoad();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        baseView.hideLoading();
                        Log.e("loginresult", result);
                        if(requestType==1)
                        {
                            messageListBean= gson.fromJson(result, MessageListBean.class);
                            if(messageListBean.getData().size()==0&&messageListView.getmyPage()==1)
                            {
                                messageListView.getNoContent().setVisibility(View.VISIBLE);
                                messageListView.getListView().setVisibility(View.GONE);
                            }else
                            {
                                messageListView.getNoContent().setVisibility(View.GONE);
                                messageListView.getListView().setVisibility(View.VISIBLE);
                                if(messageListView.getmyPage()==1)
                                {
                                    messageNoReadAdapter=new MessageNoReadAdapter(context,messageListBean);
                                    messageListView.getListView().setAdapter(messageNoReadAdapter);
                                }else
                                {
                                    messageNoReadAdapter.addData(messageListBean);
                                }
                                if(messageListBean.getTotal()<=(messageListBean.getEach()*messageListView.getmyPage()))
                                {
                                    messageListView.getListView().setPullLoadEnable(false);
                                }
                            }

                        }else if(requestType==2)
                        {

                            baseBean=gson.fromJson(result,BaseBean.class);
                            if(baseBean.getState()==0){
                                messageListView.setMyPage(1);
                                messageListView.getListView().setPullLoadEnable(true);
                                getMessage("1");
                            }else{
//                                baseView.hideLoading();
                                baseView.showError(baseBean.getMsg());
                            }

                        }

                        break;
                    case RESPONSE_FAILED:
                        baseView.hideLoading();
                        baseView.showError(result);
                        messageListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }

            } catch (Exception e)
            {
            }
        }

    };

}